<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;

class JifenGoodsController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
    
     /**
     * 会员列表
     */
    public function index(){
        $cat = M('Cat')->select();
        $this->assign('cat', $cat);
        $name = I('name') ? I('name') : null;
        $cat_id = I('cat_id') ? I('cat_id') : null;
        if (!empty($name)) {
            $where['name'] = array("like", "%" . $name . "%");
            $where['is_limit'] = 0;
        }else{
        	$where['is_limit'] = 0;
        }
        if (!empty($cat_id)) {
            $where['cat_id'] = $cat_id;
            $where['is_limit'] = 0;
        }else{
        	$where['is_limit'] = 0;
        }
        if(empty($where)){
        	$where['is_limit'] = 0;
        }
        $count = M('Goods')->where($where)->count();
        $Page = new Page($count, 8);
        setPageParameter($Page, array('name' => $name));
        $show = $Page->show();
        $list = M('Goods')->where($where)
                        ->limit($Page->firstRow . ',' . $Page->listRows)->order('goods_id desc')->select();
        $sumxiaoliang=0;
        $sumtotalmoney=0;
        foreach($list as $k=>$v){
            $go=M('order_goods_jifen');
            $ox=$go->where(array('goods_id'=>$v['goods_id']))->select();
            $totalmoney=0;
            $xiaoliang=0;
            foreach($ox as $kk =>$vv){
            	$xiaoliang+=$vv['num'];
            	$od=M('orders_jifen');
            	$olist=$od->where(array('order_id'=>$vv['order_id']))->find();
            	$totalmoney+=$olist['total_money'];
            } 
            $list[$k]['totalmoney']=$totalmoney;
            $list[$k]['xiaoliang']=$xiaoliang;
            $sumxiaoliang+=$xiaoliang;
            $sumtotalmoney+=$totalmoney; 
        }
       // dump($list);
        if(isset($_GET['xiao'])){
            $type1='xiaoliang';
            //dump($_GET);
            if($_GET['xiao']=='desc'){
                $list = $this->my_sort($list,'xiaoliang',SORT_DESC,SORT_STRING);
                $_SESSION['xiao']="asc";
            }else{
                $list = $this->my_sort($list,'xiaoliang',SORT_ASC,SORT_STRING);
                $_SESSION['xiao']="desc";
            }
        }
        if(isset($_GET['hao'])){
        	$type1='totalmoney';
        	//dump($_GET);
        	if($_GET['hao']=='desc'){
        		$list = $this->my_sort($list,'totalmoney',SORT_DESC,SORT_STRING);
        		$_SESSION['hao']="asc";
        	}else{
        		$list = $this->my_sort($list,'totalmoney',SORT_ASC,SORT_STRING);
        		$_SESSION['hao']="desc";
        	}
        }
        // dump($_SESSION);
        //dump($list); 
        $this->assign('sumxiaoliang',$sumxiaoliang);
        $this->assign('sumtotalmoney',$sumtotalmoney);
        //$this->assign('sumzlren',$sumzlren);
        $this->assign('list', $list);
        $this->assign('search', $name);
        $this->assign('page', $show);
        $this->display();
         
    }
    public function xiangqing(){
        //dump($_GET);
        $goods_id=I('goods_id');
        if($goods_id==''){
            $_SESSION['goods_id']=$_SESSION['goods_id'];
        }else{
            $_SESSION['goods_id']=$goods_id;
        }
        $goods_id=$_SESSION['goods_id'];
        $go=M('rob_goods_orders g');
        $list=$go->join('yang_rob_orders o on g.order_id=o.order_id')->join('yang_member m on m.openid=o.open_id')->where(array('goods_id'=>$goods_id))->select();
        $count=$go->join('yang_rob_orders o on g.order_id=o.order_id')->join('yang_member m on m.openid=o.open_id')->where(array('goods_id'=>$goods_id))->count();
        $this->assign('count',$count);
        
        if(isset($_GET['pqje'])){
            $type1='pay_money';
            //dump($_GET);
            if($_GET['pqje']=='desc'){
                $list = $this->my_sort($list,'pay_money',SORT_DESC,SORT_STRING);
                $_SESSION['pqje']="asc";
            }else{
                $list = $this->my_sort($list,'pay_money',SORT_ASC,SORT_STRING);
                $_SESSION['pqje']="desc";
            }
        }
        if(isset($_GET['sf'])){
            $type1='total_money';
            //dump($_GET);
            if($_GET['sf']=='desc'){
                $list = $this->my_sort($list,'total_money',SORT_DESC,SORT_STRING);
                $_SESSION['sf']="asc";
            }else{
                $list = $this->my_sort($list,'total_money',SORT_ASC,SORT_STRING);
                $_SESSION['sf']="desc";
            }
        }
        $this->assign('list',$list);
        $this->display();
    }
    public function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sort_key];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
        return $arrays;
    }


}
    
